//
//  LoginViewController.m
//  AntsExpress
//
//  Created by slccper on 16/1/3.
//  Copyright © 2016年 slccper. All rights reserved.
//

#import "LoginViewController.h"
#import "RegisteredController.h"
#import "clsOtherFun.h"
#import "clsUser.h"
@interface LoginViewController ()

@property (weak, nonatomic) IBOutlet UITextField *name;
@property (weak, nonatomic) IBOutlet UITextField *pwd;
//@property (weak, nonatomic) IBOutlet UIButton *remember;
@property (weak, nonatomic) IBOutlet UIButton *login;
@property (weak, nonatomic) IBOutlet UIButton *exit;
@property (weak, nonatomic) IBOutlet UIButton *registe;
@property (weak, nonatomic) IBOutlet UIButton *Remember;

@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.name setText:[clsOtherFun getReName]];
    [self.pwd setText:[clsOtherFun getPwd]];
    ants = [[clsAntsService alloc]init];
    ants.delegate=self;
    
    // Do any additional setup after loading the view from its nib.
}
-(void)viewDidAppear:(BOOL)animated
{
    if (![[clsOtherFun getPwd]isEqualToString:@""]) {
        self.Remember.selected=YES;
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (IBAction)loginAction:(id)sender {
    [self.view endEditing:YES];
    NSString *name =self.name.text;
    if ([name isEqualToString:@""]||name==nil) {
        [clsOtherFun ShowMsg:@"請輸入用戶名！"];
        [self.name becomeFirstResponder];
        return;
    }
    NSString *pwd =self.pwd.text;
    if ([pwd isEqualToString:@""]||pwd==nil) {
        [self.pwd becomeFirstResponder];
        [clsOtherFun ShowMsg:@"請輸入密碼！"];
        return;
    }
    if(self.Remember.selected){
        [clsOtherFun setReName:name];
        [clsOtherFun setPwd:pwd];
    }else{
        [clsOtherFun setReName:nil];
        [clsOtherFun setPwd:nil];
    }
    
    if ([clsOtherFun CheckNetworkStatus:YES]) {
        [clsOtherFun showLoadingView:@"正在登錄..."];
        [ants Login:name :pwd];
    }else{
        [clsOtherFun ShowNoNetworkMsg];
    }
    
}
- (IBAction)Remeber:(id)sender {
    self.Remember.selected = !self.Remember.selected;
}

- (IBAction)ExitAction:(id)sender {
    exit(0);
}
- (IBAction)RegisterAction:(id)sender {
    RegisteredController *regist = [[RegisteredController alloc]initWithNibName:@"RegisteredController" bundle:nil];
    regist.isBack=YES;
    [self.navigationController pushViewController:regist animated:YES];
}
- (IBAction)HideAction:(id)sender {
    [self.view endEditing:YES];
}

- (void)getAntsDataSuccess:(NSInteger)type :(NSMutableDictionary *)pDictionary
{
    
    NSDictionary *dic = [pDictionary objectForKey:@"data"];
    NSString *status = [dic objectForKey:@"status"];
    if([status integerValue]==500){
        [clsOtherFun hideLoadingView];
        [clsOtherFun ShowMsg:[dic objectForKey:@"message"]];
    }
    if ([status integerValue]==200) {
        if(type==Ants_GetUserInfo){
            [clsOtherFun hideLoadingView];
            NSDictionary *data = [dic objectForKey:@"data"];
            clsUser *user = [[clsUser alloc]initWithDic:data];
            [clsOtherFun setUser:user];
            [clsOtherFun setName:self.name.text];
            [clsOtherFun ShowMsg:@"登錄成功！"];
            [[NSNotificationCenter defaultCenter]postNotificationName:@"LoginChange" object:nil];
            [self.navigationController popViewControllerAnimated:YES];
        }else if(type ==Ants_Login){
            NSDictionary *data = [dic objectForKey:@"data"];
            [clsOtherFun setencodeName:[data objectForKey:@"encodeName"]];
            [ants GetTokenIOS:self.name.text :[clsOtherFun getDeviceToken]];
        }else if(type ==Ants_GetTokenIOS){
            [ants GetUserInfoEntity:[clsOtherFun getencodeName]];
        }
    }
}
@end
